package com.dynamixsoftware.printershare.smb.dcerpc;

import com.dynamixsoftware.printershare.smb.NtlmPasswordAuthentication;
import com.dynamixsoftware.printershare.smb.SmbFileInputStream;
import com.dynamixsoftware.printershare.smb.SmbFileOutputStream;
import com.dynamixsoftware.printershare.smb.SmbNamedPipe;
import com.dynamixsoftware.printershare.smb.util.Encdec;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

class DcerpcPipeHandle
  extends DcerpcHandle
{
  private SmbFileInputStream in = null;
  private boolean isStart = true;
  private SmbFileOutputStream out = null;
  SmbNamedPipe pipe;
  
  DcerpcPipeHandle(String paramString, NtlmPasswordAuthentication paramNtlmPasswordAuthentication)
    throws UnknownHostException, MalformedURLException, DcerpcException
  {
    this.binding = DcerpcHandle.parseBinding(paramString);
    String str1 = "smb://" + this.binding.server + "/IPC$/" + this.binding.endpoint.substring(6);
    String str2 = "";
    String str3 = (String)this.binding.getOption("server");
    if (str3 != null) {
      str2 = str2 + "&server=" + str3;
    }
    String str4 = (String)this.binding.getOption("address");
    if (str3 != null) {
      str2 = str2 + "&address=" + str4;
    }
    if (str2.length() > 0) {
      str1 = str1 + "?" + str2.substring(1);
    }
    this.pipe = new SmbNamedPipe(str1, 27198979, paramNtlmPasswordAuthentication);
  }
  
  public void close()
    throws IOException
  {
    this.state = 0;
    if (this.out != null) {
      this.out.close();
    }
  }
  
  protected void doReceiveFragment(byte[] var1, boolean var2) throws IOException {
      byte var3 = 1;
      if(var1.length < this.max_recv) {
         throw new IllegalArgumentException("buffer too small");
      } else {
         int var4;
         if(this.isStart && !var2) {
            var4 = this.in.read(var1, 0, 1024);
         } else {
            var4 = this.in.readDirect(var1, 0, var1.length);
         }

         if(var1[0] != 5 && var1[var3] != 0) {
            throw new IOException("Unexpected DCERPC PDU header");
         } else {
            if((2 & 255 & var1[3]) != 2) {
               var3 = 0;
            }

            this.isStart = (boolean)(var3 != 0);
            short var5 = Encdec.dec_uint16le(var1, 8);
            if(var5 > this.max_recv) {
               throw new IOException("Unexpected fragment length: " + var5);
            } else {
               while(var4 < var5) {
                  var4 += this.in.readDirect(var1, var4, var5 - var4);
               }

            }
         }
      }
   }
  
  
  protected void doSendFragment(byte[] paramArrayOfByte, int paramInt1, int paramInt2, boolean paramBoolean)
    throws IOException
  {
    if ((this.out != null) && (!this.out.isOpen())) {
      throw new IOException("DCERPC pipe is no longer open");
    }
    if (this.in == null) {
      this.in = ((SmbFileInputStream)this.pipe.getNamedPipeInputStream());
    }
    if (this.out == null) {
      this.out = ((SmbFileOutputStream)this.pipe.getNamedPipeOutputStream());
    }
    if (paramBoolean)
    {
      this.out.writeDirect(paramArrayOfByte, paramInt1, paramInt2, 1);
      return;
    }
    this.out.write(paramArrayOfByte, paramInt1, paramInt2);
  }
}

